home *** CD-ROM | disk | FTP | other *** search
- #include <Movies.h>
- #include <QuickTimeStreaming.h>
- #include <QTStreamingComponents.h>
- #include <FixMath.h>
-
- #include "LousyConnection.h"
-
- // data structures
-
- typedef struct {
- Component victim;
- Fixed dropRate; // 0 -> never drop, fixed1 -> always drop
- } LousyConnectionSharedGlobalsRecord, **LousyConnectionSharedGlobalsHandle;
-
- typedef struct{
- ComponentInstance self;
- ComponentInstance delegateComponent;
- ComponentInstance target;
- LousyConnectionSharedGlobalsHandle sharedGlobals;
- UInt32 eventCount;
- } LousyConnectionGlobalsRecord, *LousyConnectionGlobalsPtr;
-
- // dispatcher
-
- #define RTPRSSM_BASENAME() LousyConnection
- #define RTPRSSM_GLOBALS() LousyConnectionGlobalsPtr storage
-
- #define CALLCOMPONENT_BASENAME() RTPRSSM_BASENAME()
- #define CALLCOMPONENT_GLOBALS() RTPRSSM_GLOBALS()
-
- #define COMPONENT_UPP_PREFIX() uppRTPRssm
- #define COMPONENT_DISPATCH_FILE "LousyConnectionDispatch.h"
- #define COMPONENT_SELECT_PREFIX() kRTPRssm
-
- #define GET_DELEGATE_COMPONENT() (storage->delegateComponent)
-
- #include "QTStreamingComponents.k.h"
- #include "ComponentDispatchHelper.c"
-
- // code
-
- pascal ComponentResult LousyConnectionOpen(LousyConnectionGlobalsPtr glob, ComponentInstance self)
- {
- ComponentResult err;
- LousyConnectionSharedGlobalsHandle sharedGlobals;
-
- glob = (LousyConnectionGlobalsPtr)NewPtrClear(sizeof(LousyConnectionGlobalsRecord));
- err = MemError();
- if (noErr != err) goto bail;
-
- SetComponentInstanceStorage(self, (Handle)glob);
- glob->self = self;
- glob->target = self;
-
- sharedGlobals = (LousyConnectionSharedGlobalsHandle)GetComponentRefcon((Component)self);
- if (!sharedGlobals) {
- Component victim;
- ComponentDescription cd;
-
- GetComponentInfo((Component)self, &cd, nil, nil, nil);
- cd.componentManufacturer = kVictimManufacturer;
- cd.componentFlags = 0;
- cd.componentFlagsMask = 0;
- victim = FindNextComponent(nil, &cd);
- if (!victim) {
- DebugStr("\p couldn't find victim!");
- err = paramErr;
- goto bail;
- }
-
- sharedGlobals = (LousyConnectionSharedGlobalsHandle) NewHandleSysClear(sizeof(LousyConnectionSharedGlobalsRecord));
- if (!sharedGlobals) {
- err = MemError();
- goto bail;
- }
- SetComponentRefcon((Component)self, (long)sharedGlobals);
-
- victim = CaptureComponent(victim, (Component)self);
- if (!victim) {
- err = paramErr;
- goto bail;
- }
- (**sharedGlobals).victim = victim;
- (**sharedGlobals).dropRate = fixed1 / 2;
- }
-
- glob->sharedGlobals = sharedGlobals;
- err = OpenAComponent((**sharedGlobals).victim, &glob->delegateComponent);
- if (err) goto bail;
-
- ComponentSetTarget(glob->delegateComponent, self);
-
- bail:
- return err;
- }
-
- pascal ComponentResult LousyConnectionRegister(LousyConnectionGlobalsPtr glob)
- {
- // If we get this far, we must have been opened successfully, so all is well.
- return noErr;
- }
-
- pascal ComponentResult LousyConnectionUnregister(LousyConnectionGlobalsPtr glob)
- {
- UncaptureComponent((**glob->sharedGlobals).victim);
- DisposeHandle((Handle)glob->sharedGlobals);
- return noErr;
- }
-
- pascal ComponentResult LousyConnectionClose(LousyConnectionGlobalsPtr glob, ComponentInstance self)
- {
- if (glob) {
- if (glob->delegateComponent)
- CloseComponent(glob->delegateComponent);
-
- DisposePtr((Ptr)glob);
- }
-
- return noErr;
- }
-
- pascal ComponentResult LousyConnectionTarget(LousyConnectionGlobalsPtr glob, ComponentInstance target)
- {
- glob->target = target;
- if (glob->delegateComponent)
- ComponentSetTarget(glob->delegateComponent, target);
-
- return noErr;
- }
-
- pascal ComponentResult LousyConnectionVersion(LousyConnectionGlobalsPtr glob)
- {
- return CallComponentVersion(glob->delegateComponent);
- }
-
- static Boolean capsLockIsDown(void)
- {
- KeyMapByteArray keys;
- GetKeys((UInt32*)keys);
- return (keys[7] & 2) ? true : false;
- }
-
- pascal ComponentResult LousyConnectionHandleNewPacket(LousyConnectionGlobalsPtr glob, QTSStreamBuffer *inStreamBuffer, SInt32 inNumWraparounds)
- {
- Boolean dropThisOne = false;
- Fixed dropRate = (**glob->sharedGlobals).dropRate;
-
- dropThisOne = ((UInt16)Random()) < dropRate;
-
- if( dropThisOne ) {
- QTSFreeMessage(inStreamBuffer);
- return noErr;
- }
- else {
- return RTPRssmHandleNewPacket(glob->delegateComponent, inStreamBuffer, inNumWraparounds);
- }
- }
-